package com.loren.demo08.example03;

import org.springframework.plugin.core.PluginRegistry;
import org.springframework.plugin.core.config.EnablePluginRegistries;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Component
@EnablePluginRegistries(SmsSender.class)
public class SmsService {

    @Resource
    private PluginRegistry<SmsSender, String> smsPlugins;

    public void sendSmsMessage(String plugin) {
        Optional<SmsSender> smsPluginOptional = this.smsPlugins.getPluginFor(plugin);
        if (smsPluginOptional.isPresent()) {
            Map<String, Object> mapInfo = new HashMap<>();
            mapInfo.put("content", "hello world");
            smsPluginOptional.get().send(mapInfo);
        } else {
            System.err.println("No matching to sender");
        }
    }

}
